package com.jec.module.business.manage;

import java.nio.charset.Charset;

import com.jec.module.business.entity.Business;
import com.jec.module.business.entity.WtBusiness;
import com.jec.protocol.pdu.PDU;
import com.jec.protocol.pdu.PduConstants;
import com.jec.protocol.pdu.ProtocolUtils;
import com.jec.protocol.unit.BCD;
import com.jec.utils.Constants;

/**
 * Created by jeremyliu on 2016/11/1.
 */
public class WtManager extends BusinessManager<WtBusiness> {

    public WtManager(){
        business = new Business("外通", Constants.WtImg, PduConstants.MONITOR_BUSINESS_WT);
    }
    @Override
    protected WtBusiness pduToEntry(PDU pdu) {

        WtBusiness target = new WtBusiness();
        target.setNetunit(ProtocolUtils.getSourId(pdu));

        int offset = PduConstants.LENGTH_OF_HEAD;

        target.setNumber(pdu.getBCD(offset, PduConstants.LENGTH_OF_BCD));
        offset += PduConstants.LENGTH_OF_BCD;

        target.setName(pdu.getString(offset, PduConstants.LENGTH_OF_STR, Charset.forName("GB2312")));
        offset += PduConstants.LENGTH_OF_STR;

        target.setType(pdu.getInt8(offset++));

        target.setState(pdu.getInt8(offset++));

        target.setChairman(pdu.getBCD(offset, PduConstants.LENGTH_OF_BCD));
        offset += PduConstants.LENGTH_OF_BCD;

        int listenerCount = pdu.getInt8(offset++);
        for(int i = 0; i < listenerCount; i++) {
            BCD listener = pdu.getBCD(offset, PduConstants.LENGTH_OF_BCD);
            offset += PduConstants.LENGTH_OF_BCD;

            target.getListeners().add(listener);
        }

        int deviceCount = pdu.getInt8(offset++);
        for(int i = 0; i < deviceCount; i++) {
            BCD device = pdu.getBCD(offset, PduConstants.LENGTH_OF_BCD);
            offset += PduConstants.LENGTH_OF_BCD;

            target.getDevices().add(device);
        }
        return target;
    }
}
